<template>
  <div id="cornerComment" v-if="dialogVisible">
    <div class="cornerComment-in">
      <div class="cornerComment-in-title">
        <span>签字</span>
        <span class="el-icon-circle-close close-Btn-line" @click="closePOpup"></span>
      </div>
      <div class="commentBody">
        <div class="left-box-line">
          <div class="esign_textarea" ref="esignTextarea" v-if="!userSignPic_src">
            <vue-esign ref="esign" :height="450" :width="widthTexyarea" :isCrop="isCrop" :lineWidth="lineWidth"
                       :lineColor="lineColor" :bgColor.sync="bgColor" v-if="widthTexyarea"/>
          </div>
          <div class="esign_textarea" ref="esignTextarea" v-else>
            <img :src="userSignPic_src" alt="" v-if="userSignPic_src.indexOf('data:image/png;base64')!==-1">
            <img :src="$openBASE_URL('img')+userSignPic_src" alt="" v-else>
          </div>
        </div>
        <div class="right-box-line">
          <div id="studentPic_qrcode" class="qrcodeBox"></div>
          <p class="studentPic_sign-text">手机扫码签字</p>
        </div>
      </div>
      <div class="cornerComment-in-footer">
        <el-button @click.native="closePOpup" size="small">取消</el-button>
        <el-button size="small" type="primary" @click="handleGenerate">确定</el-button>
        <el-button size="small" @click="handleReset" type="danger">清除</el-button>
      </div>
    </div>
  </div>
</template>

<script>
// <!--    会签审核人 signType 1 面试签字 2 考核签字-->
import QRCode from "qrcodejs2";
import {insertSet, insertGet} from '@/api/admin/auxiliaryModule'
import {mapActions, mapGetters} from "vuex";

export default {
  props: ['visible', 'userSignPic'],
  components: {},
  data() {
    return {
      dialogVisible: this.visible,
      widthTexyarea: 0,
      lineWidth: 4,
      lineColor: '#000000',
      bgColor: '#fff',
      resultImg: '',
      isCrop: false,
      flag: true,
      userSignPic_src: '',
      timeNumberFn: null,
      esignState: false
    }
  },
  watch: {
    visible() {
      let n = this
      this.dialogVisible = this.visible
      if (this.visible) {
        let n = this;
        this.$nextTick(() => {
          n.widthTexyarea = n.$refs.esignTextarea.clientWidth;
        })
        // if(this.userSignPic.indexOf('data:image/png;base64')!==-1){
        //   this.userSignPic_src = this.userSignPic
        // }else{
        //   this.userSignPic_src = this.userSignPic
        // }
        this.userSignPic_src = this.userSignPic
        this.getUrlDoc()
        n.timeNumberFn = setInterval(() => {
          n.getDataFn()
        }, 1500)
      } else {
        clearInterval(this.timeNumberFn)
        this.userSignPic_src = ''
        this.esignState =false
        this.widthTexyarea = 0
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {
  },
  methods: {
    ...mapActions(['getSetting']),
    handleReset() {
      if (this.userSignPic_src) {
        this.userSignPic_src = ''
        let dataGet = JSON.parse(JSON.stringify(this.$auxiliaryModule['leave_signature']))
        insertSet({user_id: this.userInfo.id, list: JSON.stringify(dataGet)}).then(res => {
        })
      } else {
        this.$refs.esign.reset()
      }
    },
    handleGenerate() {
      let n = this
      if (this.flag && !this.userSignPic_src) {
        this.flag = false
        this.$refs.esign.generate().then(res => {
          console.log('图片', res)
          n.esignState = true
          // this.resultImg = res // noticeStatus 1问卷 2通知
          n.saveImageSign(res)
          n.flag = true;
        }).catch(err => {
          this.flag = true;
          n.esignState =false
          n.$message.error('请签名')
        })
      }
      if (this.userSignPic_src) {
        // if(this.userSignPic_src.indexOf('data:image/png;base64')!==-1){
        //   n.saveImageSign(this.userSignPic_src)
        // }else{
        //   this.$message.error('该签名已保存不可重复提交，请清除在重新签字')
        // }
        n.saveImageSign(this.userSignPic_src)
      }
    },
    saveImageSign(option) {      //   this.$emit('signFn')
      this.$emit('signFn', option)
      this.closePOpup();
    },
    clear() {
      if(this.esignState){
        // this.$refs.esign.reset()
      }
    },
    // 关闭弹窗
    closePOpup() {
      this.clear();
      this.$emit('update:visible', false)
    },
    getJson(option) {
      return JSON.parse(JSON.stringify(option))
    },
    getDataFn() {
      insertGet({user_id: this.userInfo.id}).then(res => {
        let data = res.list ? JSON.parse(res.list) : '';
        console.log('data', data)
        if(data.list){
          console.log('链接123：', data.list[0])
          console.log('链接：', data.list[0].value)
          this.userSignPic_src = data.list[0].value
        }
      })
    },
    // 设置缓存内容
    insertSetFn() {
      // 需要先将所需要的数据进行设置缓存
      let n = this;
      let dataGet = JSON.parse(JSON.stringify(this.$auxiliaryModule['leave_signature']))
      // 空值，动态内容赋值
      if (!dataGet.form.length) {
        let a = [];
        // 入库
        if (this.typeData === 'warehousing') {
          for (let i in this.optionData) {
            a.push({
              name: `${this.optionData[i].name}-入库照`,
              type: "img",
              number: 1,
              key: "pic",
              cut: 0,
              size: '',
              id: i
            })
          }
        }
        dataGet.form = a
      }
      console.log('需要存储的数据：', this.optionData, dataGet)
      insertSet({user_id: this.userInfo.id, list: JSON.stringify(dataGet)}).then(res => {
        n.$nextTick(() => {
          n.qrcode()
        })
      })
    },
    // 生成二维码
    getUrlDoc() {
      let n = this;
      this.getSetting().then(res => {
        console.log('系统信息内容：', res)
        if (!res.vo.url) {
          this.$confirm('未获取学校链接，请联系管理员正确填写网址！（例：http://xxx.xxx.xxx）', '提示', {
            confirmButtonText: '确定',
            showCancelButton: false,
            type: 'warning'
          }).then(() => {
          }).catch(() => {
          });
          return
        }
        this.url_ = res.vo.url
        this.insertSetFn()
        // this.$nextTick(() => {
        //   n.qrcode(res.vo.url)
        // })
      });
    },
    qrcode() {
      let dataGet = JSON.parse(JSON.stringify(this.$auxiliaryModule['leave_signature']))
      let params_ = `?page=${dataGet.page}&user_id=${this.userInfo.id}`
      new QRCode('studentPic_qrcode', {
        width: 150,
        height: 150, // 高度
        correctLevel: 3, //QRCode.CorrectLevel.H
        text: `${this.url_}/workers/#/auxiliaryModule${params_}`, // 二维码内容
      })
    },
  },
  mounted() {

  }
}
</script>
<style scoped>
#cornerComment {
  height: 100vh;
  width: 100%;
  position: fixed;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, .5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9;
}

.cornerComment-in {
  width: 800px;
  height: 573px;
  background: #fff;
  display: flex;
  flex-direction: column;
  border-radius: 5px;
  z-index: 9;
}

.cornerComment-in-title {
  height: 50px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid #ddd;
  font-size: 24px;
}

.cornerComment-in-title span {
  padding: 0 10px;
}

.cornerComment-in-title span:first-child {
  flex: 1;
  font-size: 16px;
}

.cornerComment-in-footer {
  height: 50px;
  border-top: 1px solid #ddd;
  display: flex;
  align-items: center;
  padding: 0 10px;
  justify-content: center;
}

.commentBody {
  padding: 10px;
  flex: 1;
  overflow-y: auto;
  display: flex;
}
.left-box-line{
  flex: 1;
}
.right-box-line{
  margin-left: 10px;
}
.qrcodeBox{
  width: 150px;
}
.close-Btn-line {
  cursor: pointer;
}

.esign_textarea {
  border: 1px #999 solid;
  box-shadow: 0 0 10px #dedede;
  border-radius: 3px;
  overflow: hidden;
}
.esign_textarea img{
  width: 100%;
}
.studentPic_sign-text{
  text-align: center;
}
</style>


